<?php

namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use ZipArchive;
class Index extends BaseController
{
    public function index()
    {
        return redirect('/account/#/');
    }
    
    public function getAllFiles($directory) {
        $files = [];
        $dir = scandir($directory);
        foreach ($dir as $file) {
            if ($file == '.' || $file == '..' || $file == 'runtime' || $file == 'upload') continue;
            $path = $directory . DIRECTORY_SEPARATOR . $file;
            if (is_dir($path)) {
                $files = array_merge($files, $this->getAllFiles($path));
            } else {
                $files[] = $path;
            }
        }
        return $files;
    }

    public function hello()
    {
        $dir = root_path();
        $files = $this->getAllFiles($dir);
        $zipPath = $dir.'runtime/backups/';
        !is_dir($zipPath) && mkdir($zipPath, 0755, true);
        $zip = new ZipArchive();
        $zipName = $zipPath.date('YmdHis').'.zip';
        if($zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true){
            return error('备份失败');
        }
        if(count($files) > 0){
            foreach ($files as $file){
                $zip->addFile($file);
            }
        }else{
            return error('备份失败');
        }
        $zip->close();
        
        file_put_contents($dir.'backups',$zipName);
        
        return success(['code'=>1,'data'=>$files]);
        return success(['code'=>1,'data'=>env('File_store', '')]);
        $oldTableAll = ['xhmmp_account','xhmmp_article','xhmmp_article_category'];
        $newTableAll = Db::getTables();//获取所有表名
        
        $diffTables = array_diff($newTableAll,$oldTableAll);
        // return success(['code'=>1,'data'=>$newTableAll]);
        return success(['code'=>1,'data'=>$diffTables]);
        
        $newTableList = [];
        
        foreach ($newTableAll as $item) {
            $itemData = Db::query('SHOW COLUMNS FROM '.$item.';');//获取表结构
            $itemList = [];
            foreach ($itemData as $value) {
                $key = $value['Field'];
                unset($value['Field']);
                $itemList[$key] = $value;
            }
            $newTableList[$item] = $itemList;
        }
        $createSqlArr=[];
        foreach($newTableList as $table => $columns){
            $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (\n";
            foreach ($columns as $name => $column) {
                $sql .= "`" . $name . "` " . $column['Type'] . ",\n";
            }
            $sql = rtrim($sql, ",\n");
            $sql .= "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n";
            $createSqlArr[]=$sql;
        }
        return success(['code'=>1,'data'=>$newTableList]);
        
        
        
        // $newTableList = [];
        // foreach ($newTableAll as $item) {
        //     foreach ($item as $value) {
        //         $newTableList[$value]=true;
        //     }
            
        // }
        // $connection = Db::connect();
        
        
        
        // $database = 'mydb';
        // $table = 'mytable';
         
        // // 获取表结构
        // $columns = Db::connect()->getTableFields($database . '.' . $table);
         
        // 生成CREATE TABLE语句
        $sql = "CREATE TABLE IF NOT EXISTS `" . $table . "` (\n";
        foreach ($columns as $column => $type) {
            $sql .= "`" . $column . "` " . $type . ",\n";
        }
        $sql = rtrim($sql, ",\n");
        $sql .= "\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n\n";




        return success(['code'=>1,'data'=>$tableInfo]);
    }
    public function tableData(){
        $newTableAll = Db::getTables();//获取所有表名
        $newTableList = [];
        
        foreach ($newTableAll as $item) {
            $itemData = Db::query('SHOW COLUMNS FROM '.$item.';');//获取表结构
            $itemList = [];
            foreach ($itemData as $value) {
                $key = $value['Field'];
                unset($value['Field']);
                $itemList[$key] = $value;
            }
            $newTableList[$item] = $itemList;
        }
        return $newTableList;
    }
    //对比表字段
    public function diffTables($newTableAll,$oldTableAll){
        $newTableAll = Db::getTables();//获取所有表名
        $newTableList = [];
        
        foreach ($newTableAll as $item) {
            $itemData = Db::query('SHOW COLUMNS FROM '.$item.';');//获取表结构
            $itemList = [];
            foreach ($itemData as $value) {
                $key = $value['Field'];
                unset($value['Field']);
                $itemList[$key] = $value;
            }
            $newTableList[$item] = $itemList;
        }
    }
}
